L2-039 清点代码库
题目 L2-039 清点代码库
思路分析
map计数
转为vector 排序
代码实现
算是投机取巧 直接输出带空格的string 而不是多个int
18/25
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using Pll = pair<ll, ll>;
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
const int inf = 0x3f3f3f3f;
using PSI = pair<string,int>;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// 若输入相同 输出相同 则重复
// 输出简化为整数
// n m
int n,m;
cin>>n>>m;
unordered_map<string,int> hx;
while(n--) {
string s;
for(int i=0; i<m; i++) {
int tmp;
cin>>tmp;
s+=" ";
s+=to_string(tmp);
}
hx[s]++;
}
cout<<hx.size()<<endl;
vector<PSI> res(hx.begin(),hx.end());
sort(res.begin(),res.end(),[](const PSI& a,const PSI& b) {
if (a.second != b.second) return a.second > b.second;
return a.first < b.first;
});
for(auto v:res) {
cout<<v.second<<v.first<<endl;
}
return 0;
}
其实也可以直接用数组为键
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using Pll = pair<ll, ll>;
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
const int inf = 0x3f3f3f3f;
using PIV = pair<int,vector<int>>;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n,m;cin>>n>>m;
map<vector<int>,int> hx;
while(n--) {
vector<int> cur(m);
for(int i=0; i<m; i++) {
cin>>cur[i];
}
hx[cur]++;
}
cout<<hx.size()<<endl;
vector<PIV> res;
for(auto v:hx){
res.push_back({v.second,v.first});
}
sort(res.begin(),res.end(),[](const PIV& a,const PIV& b){
if(a.first!=b.first) return a.first>b.first;
return a.second<b.second;
});
for(auto v:res){
cout<<v.first;
for(auto n:v.second) cout<<" "<<n;
cout<<endl;
}
return 0;
}
同类题型
视频讲解
⬅️ L2-038 病毒溯源 🏠 00-天梯赛 ➡️ L2-040 哲哲打游戏
💬 评论